<template>
  <div class="table-box">
    <ProTable :columns="columns" ref="proRef" :requestApi="getAll" :initParam="initParam">
      <template #tableHeader>
        <el-button type="primary" @click="opera(undefined)">创建优惠活动</el-button>
        <el-button type="danger">批量删除</el-button>
      </template>
      <template #opera="{ row }">
        <el-button link :icon="EditPen" type="primary" @click="opera(row)">编辑</el-button>
        <el-button link :icon="Delete" type="primary">删除</el-button>
      </template>
    </ProTable>
    <model-activity-drawer ref="acRef"></model-activity-drawer>
  </div>
</template>

<script setup lang="ts">
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps } from "@/components/ProTable/interface";
import ModelActivityDrawer from "../components/modelActivityDrawer.vue";
import { EditPen, Delete } from "@element-plus/icons-vue";
import { reactive, ref, onBeforeMount } from "vue";
import { createActivity, editActivity, getAll, getRegion } from "@/api/modules/modelActivity";

const regions = ref();

const getRegions = async () => {
  const { data } = await getRegion();
  regions.value = data;
};

const proRef = ref<InstanceType<typeof ProTable> | null>();

const acRef = ref<InstanceType<typeof ModelActivityDrawer> | null>();

const initParam = reactive({ pageSize: 10, pageNum: 1 });

const columns: ColumnProps[] = [
  { prop: "sort", label: "排序值" },
  { prop: "initNum", label: "初始人数" },
  { prop: "title", label: "活动标题" },
  { prop: "application", label: "报名人数" },
  { prop: "opera", label: "操作" }
];

const opera = (row: any) => {
  const params: any = {
    row,
    api: row ? editActivity : createActivity,
    getTable: proRef.value?.getTableList,
    city: regions.value
  };
  acRef.value?.acceptParams(params);
};

onBeforeMount(() => {
  Promise.all([getRegions()]);
});
</script>

<style></style>
